<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>后台管理系统</h2>
        <p class="subtitle">欢迎登录</p>
      </div>
      <form @submit.prevent="handleLogin">
        <div class="form-item">
          <div class="input-wrapper">
            <i class="icon-user"></i>
            <input type="text" v-model="loginForm.account" required placeholder="请输入管理员账号">
          </div>
        </div>
        <div class="form-item">
          <div class="input-wrapper">
            <i class="icon-lock"></i>
            <input type="password" v-model="loginForm.password" required placeholder="请输入管理员密码">
          </div>
        </div>
        <div class="form-actions">
          <button type="submit">登 录</button>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive } from 'vue'
import { useRouter } from 'vue-router'
import { login } from '@/api/user'

const router = useRouter()

const loginForm = reactive({
  account: '',
  password: ''
})

const handleLogin = async () => {
  try {
    const res = await login(loginForm)
    console.log('登录响应:', res)
    // token字段是小写的
    const token = res.data.token
    console.log('获取到的token:', token)
    if (token) {
      localStorage.setItem('token', token)
      router.push('/dashboard/user')
    } else {
      throw new Error('登录失败：未获取到token')
    }
  } catch (error: any) {
    console.error('登录错误:', error)
    alert(error.message)
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
  position: fixed;
  top: 0;
  left: 0;
}

.login-box {
  width: 400px;
  padding: 40px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  backdrop-filter: blur(10px);
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

h2 {
  margin: 0;
  color: #1890ff;
  font-size: 28px;
  font-weight: 600;
  letter-spacing: 1px;
}

.subtitle {
  margin: 10px 0 0;
  color: #666;
  font-size: 16px;
}

.form-item {
  margin-bottom: 24px;
}

.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
  background: #f5f7fa;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.input-wrapper:focus-within {
  background: white;
  box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
}

.icon-user, .icon-lock {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #909399;
}

.icon-user::before {
  content: "👤";
  font-size: 18px;
}

.icon-lock::before {
  content: "🔒";
  font-size: 18px;
}

input {
  flex: 1;
  padding: 12px 15px;
  border: none;
  background: transparent;
  font-size: 14px;
  color: #606266;
}

input::placeholder {
  color: #909399;
}

input:focus {
  outline: none;
}

.form-actions {
  margin-top: 40px;
}

button {
  width: 100%;
  padding: 12px;
  background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  letter-spacing: 4px;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(24, 144, 255, 0.3);
}

button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(24, 144, 255, 0.4);
}

button:active {
  transform: translateY(0);
  box-shadow: 0 2px 8px rgba(24, 144, 255, 0.3);
}
</style> 